const express = require('express');
const router = express.Router();
const roleController = require('../../controllers/roleController');
const { query,param, body } = require('express-validator');
const { validateRequest } = require("../../middleware/validationMiddleware");

// 获取角色列表
router.get('/role/page', [
  query('pageNum').isInt({min: 1}).withMessage('页码必须为正整数'),
  query('pageSize').isInt({min: 1, max: 100}).withMessage('每页条数必须为1-100之间的整数'),
  query('keywords').optional(),
  query('status').optional().isIn([0, 1]).withMessage('状态值无效'),
], validateRequest,
roleController.getRoleList);

// 获取角色下拉数据
router.get('/role/options', roleController.getRoleOptions);

// 获取角色详情
router.get('/role/:id', roleController.getRoleById);

// 创建角色
router.post('/role/',
  [
    body('name').notEmpty().withMessage('角色名称不能为空'),
    body('status').isInt({min: 0, max: 1}).withMessage('状态值无效'),
    body('sort').optional().isInt().withMessage('排序值必须为整数'),
  ],
  validateRequest,
  roleController.createRole
);

// 更新角色
router.put('/role/:id',
  [
    body('name').optional(),
    body('code').optional(),
    body('status').optional().isInt({min: 0, max: 1}).withMessage('状态值无效'),
    body('sort').optional().isInt().withMessage('排序值必须为整数'),
  ],
  validateRequest,
  roleController.updateRole
);

// 删除角色
router.delete('/role/:ids',
  [
    param('ids').notEmpty().withMessage('角色ids不能为空')
  ],
  validateRequest,
  roleController.deleteRoles);

// 分配角色菜单权限
router.put('/role/:roleId/menus',
  [
    body('menuIds').isArray().withMessage('菜单ID必须为数组'),
  ],
  validateRequest,
  roleController.assignRoleMenus
);

// 获取角色菜单权限
router.get('/role/:roleId/menuIds', roleController.getRoleMenuIds);

module.exports = router;